﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Transitions;

namespace UIT_CV_Management
{
    public partial class LoginForm : Form
    {
        Settings setting;

        public LoginForm()
        {
            InitializeComponent();
            //Load settings
            setting = new Settings();
            Language.CurrentLanguage = Language.LanguageCode(setting.Language);
            //Init language
            InitLanguage();
        }

        /// <summary>
        /// Khởi tạo ngôn ngữ cho form
        /// </summary>
        public void InitLanguage()
        {
            this.Text = Language._("LOGIN_FORM_TITLE");
            lblTitle.Text = Language._("LOGIN_FORM_TITLE_LBL");
            lblUsername.Text = Language._("LOGIN_FORM_USERNAME_LBL");
            lblPassword.Text = Language._("LOGIN_FORM_PASSWORD_LBL");
            lblNewUser.Text = Language._("LOGIN_FORM_NEWUSER_LBL");
            lblRegister.Text = Language._("LOGIN_FORM_NEWUSER_HERE_LBL");
            btnLogin.Text = Language._("LOGIN_FORM_LOGIN_BTN");
            btnExit.Text = Language._("LOGIN_FORM_EXIT_BTN");
            lblRegister.Left = lblNewUser.Left + lblNewUser.Width -5;
            if (Language.CurrentLanguage == "vi")
                btnLanguage.Image = new Bitmap(UIT_CV_Management.Properties.Resources.us);
            else
                btnLanguage.Image = new Bitmap(UIT_CV_Management.Properties.Resources.vi);
            ttpMain.SetToolTip(btnLanguage, Language._("LOGIN_FORM_LANGUAGE_CHANGE_TOOLTIP"));
        }
        /// <summary>
        /// Đổi màu label Here khi chuột rê lên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lblRegister_MouseMove(object sender, MouseEventArgs e)
        {
            lblRegister.ForeColor = Color.Blue;
        }

        /// <summary>
        /// Đổi màu label Here khi chuột rê lên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lblRegister_MouseLeave(object sender, EventArgs e)
        {
            lblRegister.ForeColor = Color.Black;
        }

        /// <summary>
        /// Thoát
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// HIên thị form Register
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lblRegister_Click(object sender, EventArgs e)
        {
            RegisterForm regForm = new RegisterForm();
            Int32 TotalWidth = this.Width + regForm.Width;
            Int32 CurrentLeft = this.Left;
            Int32 iMoveLeft = (Screen.PrimaryScreen.Bounds.Width - TotalWidth) / 2;
            Transition.run(this, "Left", iMoveLeft, new TransitionType_EaseInEaseOut(200));
            regForm.Left = iMoveLeft + this.Width;
            regForm.Top = this.Top;
            regForm.ShowDialog();
            Transition.run(this, "Left", CurrentLeft, new TransitionType_EaseInEaseOut(200));
        }
        /// <summary>
        /// Hiển thị mainForm khi đăng nhập thành công
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            // Kiểm tra đăng nhập
            User CurrentUser = new User(tbxUserName.Text);
            if (CurrentUser.Login(tbxPassword.Text))
            {
                //Đăng nhập thành công
                currentUser.logedinUser = CurrentUser;
                MainForm mainForm = new MainForm();
                this.Hide();
                mainForm.Show();
            }
            else
            {
                //Đăng nhập thất bại
                wMessageBox.Show(this, Language._("LOGIN_FORM_LOGIN_FAIL_MSG"), Language._("LOGIN_FORM_LOGIN_FAIL_TITLE"), 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Thay đổi ngôn ngữ
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLanguage_Click(object sender, EventArgs e)
        {
            if (Language.CurrentLanguage == "en")
                Language.CurrentLanguage = "vi";
            else
                Language.CurrentLanguage = "en";

            setting.Language = Language.LanguageName(Language.CurrentLanguage);
            setting.Save();
            InitLanguage();
        }
    }
}
